package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ethink.id.generator.IdGenerator;
import com.ethink.order.api.enums.TobSaleOrderOrderStatusEnum;
import com.ethink.order.api.info.TobSaleDetailOrderInfo;
import com.ethink.order.api.request.TobOrderSettleUpdateRequest;
import com.ethink.order.api.request.TobSaleDetailOrderAddRequest;
import com.ethink.order.api.request.TobSaleDetailOrderUpdateRequest;
import com.ethink.order.api.utils.EthinkStrUtil;
import com.ethink.order.common.entity.TobSaleDetailOrder;
import com.ethink.order.common.entity.TobSaleOrder;
import com.ethink.order.common.mapper.TobSaleDetailOrderMapper;
import com.ethink.order.common.mapper.TobSaleOrderMapper;
import com.ethink.order.manager.PublicManager;
import com.ethink.order.service.TobSaleDetailOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TOB销售详单 Service接口实现
 *
 * @author default
 * @date 2022-11-16
 */
@Slf4j
@Service
public class TobSaleDetailOrderServiceImpl extends ServiceImpl<TobSaleDetailOrderMapper, TobSaleDetailOrder> implements TobSaleDetailOrderService {
    @Autowired
    private TobSaleDetailOrderMapper tobSaleDetailOrderMapper;


    @Autowired
    private TobSaleOrderMapper tobSaleOrderMapper;

    @Autowired
    private PublicManager publicManager;

    @Autowired
    private IdGenerator idGenerator;

    @Override
    public void batchInsert(List<TobSaleDetailOrderAddRequest> item, String orderNo) {
        for (TobSaleDetailOrderAddRequest tobSaleDetailOrderAddRequest : item) {
            addTobSaleDetailOrder(tobSaleDetailOrderAddRequest, orderNo);
        }
    }

    @Override
    public void addTobSaleDetailOrder(TobSaleDetailOrderAddRequest request, String orderNo) {
        TobSaleDetailOrder tobSaleDetailOrder = new TobSaleDetailOrder();
        String detailOrderNo = idGenerator.tobDetail();
        BeanUtils.copyProperties(request, tobSaleDetailOrder);
        tobSaleDetailOrder.setOrderNo(orderNo);
        tobSaleDetailOrder.setDetailOrderNo(detailOrderNo);
        String cateName = EthinkStrUtil.stitchStrings(request.getGeneraCodeName(), request.getMinorDefectsCodeName(), request.getSubclassCodeName());
        tobSaleDetailOrder.setCategoryName(cateName);
        tobSaleDetailOrder.setGrossProfitRate(publicManager.computeRate(request.getSalePrice(), request.getCostPrice(), 6));
        tobSaleDetailOrderMapper.insert(tobSaleDetailOrder);
    }

    @Override
    public void bulkUpdate(List<TobSaleDetailOrderUpdateRequest> item) {
        List<TobSaleDetailOrder> details = new ArrayList<>();
        TobSaleDetailOrder tobSaleDetailOrder;
        for (TobSaleDetailOrderUpdateRequest tobSaleDetailOrderUpdateRequest : item) {
            tobSaleDetailOrder = new TobSaleDetailOrder();
            BeanUtil.copyProperties(tobSaleDetailOrderUpdateRequest, tobSaleDetailOrder);
            details.add(tobSaleDetailOrder);
        }
        updateBatchById(details);
    }

    @Override
    public List<TobSaleDetailOrderInfo> queryDetailOrderByOrderNo(String orderNo) {
        return tobSaleDetailOrderMapper.selectTobSaleDetailByOrderNo(orderNo);
    }

    @Override
    public void batchSettleDetail(List<TobOrderSettleUpdateRequest> requestList) {
        if (CollUtil.isEmpty(requestList)) {
            return;
        }
        for (TobOrderSettleUpdateRequest settleUpdateRequest : requestList) {
            tobSaleDetailOrderMapper.update(null, new LambdaUpdateWrapper<TobSaleDetailOrder>()
                    .eq(TobSaleDetailOrder::getId, settleUpdateRequest.getId())
                    .setSql("settle_qty = settle_qty + " + settleUpdateRequest.getSettleQty())
                    .set(TobSaleDetailOrder::getIsSettle, settleUpdateRequest.getIsSettle()));
        }
    }

}
